<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Model;

/**
 * Class FacilityModel
 * @package App\Models
 * @version May 10, 2018, 2:24 pm CST
 *
 * @property smallint category_id
 */
class FacilityModel extends CommonModel
{
    public $table = 'facility';
    
    public $fillable = [
        'category_id',
        'city_id',
        'city_name',
        'food_brand',
        'price',
        'name',
        'address',
        'working_time',
        'tel',
        'description',
        'images',
        'brand',
        'status',
    ];

    public $searchable = [
        'category_id',
        'city_id',
        'city_name',
        'food_brand',
        'price',
        'name',
        'address',
        'working_time',
        'tel',
        'description',
        'images',
        'brand',
        'status',
    ];

    public $changeable = [
        'category_id',
        'city_name',
        'city_id',
        'food_brand',
        'price',
        'name',
        'address',
        'working_time',
        'tel',
        'description',
        'images',
        'brand',
        'status',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'category_id' => 'required�|int'
    ];


    /**
     * @param $searchOptions
     * @return mixed
     */
    public function getQueryBySearchOptions($searchOptions)
    {
        $qb = self::newQuery();

        if (empty($searchOptions)) {
            return $qb;
        }

        !empty($searchOptions['keywords']) && $qb = $qb
            ->where('name', 'like', '%' . $searchOptions['keywords']. '%');
        unset($searchOptions['keywords']);

        foreach ($searchOptions as $key => $value) {
            if (is_array($value)) {
                $qb->whereIn($key, $value);
            } elseif (is_string($value) || is_numeric($value)) {
                $qb->where($key, $value);
            }
        }

        return $qb;
    }
}

